$package('Bdth');
var Bdth = {
	/*Json 工具类*/
	isJson: function (str) {
		var obj = null;
		try {
			obj = Bdth.paserJson(str);
		} catch (e) {
			return false;
		}
		var result = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
		return result;
	},
	paserJson: function (str) {
		return eval("(" + str + ")");
	},
	/*弹出框*/
	alert: function (title, msg, icon, callback) {
		$.iMessager.alert(title, msg, icon, callback);
	},
	/*弹出框*/
	confirm: function (title, msg, callback) {
		$.iMessager.confirm(title, msg, callback);
	},
	progress: function (title, msg) {
		var win = $.iMessager.progress({
			title: title || '请稍等...',
			msg: msg || '正在加载中...'
		});
	},
	closeProgress: function () {
		$.iMessager.progress('close');
	},
	/*重新登录页面*/
	toLogin: function () {
		window.top.location = path + "/login";
	},
	checkLogin: function (data) {//检查是否登录超时
		if (data.logoutFlag) {
			Bdth.closeProgress();
			Bdth.alert('提示', "登录超时,请重新登录.", 'messager-error', Bdth.toLogin);
			return false;
		}
		return true;
	},
	ajaxSubmit: function (form, option) {
		form.ajaxSubmit(option);
	},
	ajaxJson: function (url, option, callback) {
		$.ajax(url, {
			type: 'post',
			dataType: 'json',
			data: option,
			success: function (data) {
				//坚持登录
				if (!Bdth.checkLogin(data)) {
					return false;
				}
				if ($.isFunction(callback)) {
					callback(data);
				}
			},
			error: function (response, textStatus, errorThrown) {
				try {
					Bdth.closeProgress();
					var data = $.parseJSON(response.responseText);
					//检查登录
					if (!Bdth.checkLogin(data)) {
						return false;
					} else {
						Bdth.alert('提示', data.msg || "请求出现异常", 'messager-error');
					}
				} catch (e) {
					Bdth.alert('提示', "请求出现异常.", 'messager-error');
				}
			},
			complete: function () {

			}
		});
	},
	submitForm: function (form, data, callback, dataType) {
		if ('text' == dataType) {
			var option =
			{
				type: 'post',
				dataType: dataType || 'text',
				data: data,
				success: function (data) {
					var start = data.indexOf(">");
					if (start != -1) {
						var end = data.indexOf("<", start + 1);
						if (end != -1) {
							data = data.substring(start + 1, end);
						}
					}
					eval("data = " + data);
					data = eval(data);
					if ($.isFunction(callback)) {
						callback(data);
					}
				},
				error: function (response, textStatus, errorThrown) {
					try {
						Bdth.closeProgress();
						var data = $.parseJSON(response.responseText);
						//检查登录
						if (!Bdth.checkLogin(data)) {
							return false;
						} else {
							Bdth.alert('提示', data.msg || "请求出现异常", 'messager-error');
						}
					} catch (e) {
						Bdth.alert('提示', "请求出现异常.", 'messager-error');
					}
				},
				complete: function () {

				}
			}
		} else {
			var option =
			{
				type: 'post',
				dataType: dataType || 'json',
				data: data,
				success: function (data) {
					if ($.isFunction(callback)) {
						callback(data);
					}
				},
				error: function (response, textStatus, errorThrown) {
					try {
						Bdth.closeProgress();
						var data = $.parseJSON(response.responseText);
						//检查登录
						if (!Bdth.checkLogin(data)) {
							return false;
						} else {
							Bdth.alert('提示', data.msg || "请求出现异常", 'messager-error');
						}
					} catch (e) {
						Bdth.alert('提示', "请求出现异常.", 'messager-error');
					}
				},
				complete: function () {

				}
			}
		}
		Bdth.ajaxSubmit(form, option);
	},
	saveForm: function (form, data, callback, dataType) {
		if (form.form('validate')) {
			Bdth.progress('请稍等', '保存中...');
			//ajax提交form
			Bdth.submitForm(form, data, function (data) {
				Bdth.closeProgress();
				if (data.success) {
					if (callback) {
						callback(data);
					} else {
						Bdth.alert('提示', '保存成功.', 'messager-info');
					}
				} else {
					Bdth.alert('提示', data.msg, 'messager-error');
				}
			}, dataType);
		}
	},
	/**
	 *
	 * @param {} url
	 * @param {} option {id:''}
	 */
	getById: function (url, option, callback) {
		Bdth.progress();
		Bdth.ajaxJson(url, option, function (data) {
			Bdth.closeProgress();
			if (data.success) {
				if (callback) {
					callback(data);
				}
			} else {
				Bdth.alert('提示', data.msg, 'messager-error');
			}
		});
	},
	deleteForm: function (url, option, callback) {
		Bdth.progress();
		Bdth.ajaxJson(url, option, function (data) {
			Bdth.closeProgress();
			if (data.success) {
				if (callback) {
					callback(data);
				}
			} else {
				Bdth.alert('提示', data.msg, 'messager-error');
			}
		});
	}
}

/* 自定义密码验证*/
$.extend($.fn.validatebox.defaults.rules, {
	equals: {
		validator: function (value, param) {
			return value == $(param[0]).val();
		},
		message: '内容不匹配.'
	}
});

/*表单转成json数据*/
$.fn.serializeObject = function () {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function () {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [o[this.name]];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
}

/* easyui datagrid 添加和删除按钮方法*/
$.extend($.fn.datagrid.methods, {
	addToolbarItem: function (jq, items) {
		return jq.each(function () {
			var toolbar = $(this).parent().prev("div.datagrid-toolbar");
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				if (item === "-") {
					toolbar.append('<div class="datagrid-btn-separator"></div>');
				} else {
					var btn = $("<a href=\"javascript:void(0)\"></a>");
					btn[0].onclick = eval(item.handler || function () {
						});
					btn.css("float", "left").appendTo(toolbar).linkbutton($.extend({}, item, {plain: true}));
				}
			}
			toolbar = null;
		});
	},
	removeToolbarItem: function (jq, param) {
		return jq.each(function () {
			var btns = $(this).parent().prev("div.datagrid-toolbar").children("a");
			var cbtn = null;
			if (typeof param == "number") {
				cbtn = btns.eq(param);
			} else if (typeof param == "string") {
				var text = null;
				btns.each(function () {
					text = $(this).data().linkbutton.options.text;
					if (text == param) {
						cbtn = $(this);
						text = null;
						return;
					}
				});
			}
			if (cbtn) {
				var prev = cbtn.prev()[0];
				var next = cbtn.next()[0];
				if (prev && next && prev.nodeName == "DIV" && prev.nodeName == next.nodeName) {
					$(prev).remove();
				} else if (next && next.nodeName == "DIV") {
					$(next).remove();
				} else if (prev && prev.nodeName == "DIV") {
					$(prev).remove();
				}
				cbtn.remove();
				cbtn = null;
			}
		});
	}
});